javascript:
is_jQuery();
function rd2(min,max){return (Math.random()*(max-min)+min);}
function rd(min,max){return Math.floor(Math.random()*(max-min)+min);}
function rt(){return parseFloat((rd2(0,1)).toFixed(6))}
function is_jQuery() {
    if (window.jQuery == undefined) {
        var add_jQuery = document.createElement('script');
        add_jQuery.src = "//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js";
        window.jQuery || document.getElementsByTagName('head')[0].appendChild(add_jQuery);
        setTimeout(is_jQuery, 200);
    } else {       
		get_total_hour();
		setTimeout(()=> index(1),200);
    }
}
var begin_time,watching_time,pause_time,mag = 10,all_classhours = 0,watching_title,req_err = 0;;
function get_total_hour() {
	pause_time = new Date().getTime();
    $.ajax({
        type: "POST",
        url: "https://xzzf.gxrcpx.com:4443/api/internal.Train/index",
        data: 'r='+new Date().getTime(),
		dataType:"json",
        error: function (obj) {
			console.log(JSON.stringify(obj));
        },
        success: function (obj) {
			all_classhours = obj.data && obj.data.total_hour;
			console.log('当前获得课时'+all_classhours);
        }
    });
}
function index(page) {
    $.ajax({
        type: "POST",
        url: "https://xzzf.gxrcpx.com:4443/api/internal.Train/course",
        data: 'realmid=0&page='+page+'&type=1&status=&r='+new Date().getTime(),
		dataType:"json",
        error: function (obj) {
			console.log(JSON.stringify(obj));
			if(req_err<5){
				setTimeout(()=>index(page),rd(1000,2000));
			}
        },
        success: function (obj) {
			var list = obj.data && obj.data.data;
			var this_page_complete = 1;
			if(list && list.length>0){
				for(var i = 0;i<list.length;i++){
					var {title} = list[i];
					var {id}= list[i];
					var {percentage} = list[i];
					if(percentage==null){percentage = 0;}
					var {classhours_user} = list[i];
					var {classhours} = list[i];
					
					if(all_classhours<20 && percentage<100){
						console.log('课程名称',title,'课程id',id,'完成进度',percentage,'当前课程可获得课时',classhours,'当前课程已获得课时',classhours_user,'全部课程获得总课时',all_classhours);
						this_page_complete = 0;						
						watching_title = title;
						setTimeout(()=>begin(id),rd(500,1000));
						break;
					}
				}
				if(this_page_complete==1){
					console.log('此页课程已完成，到下页');
					setTimeout(()=>{
						page++;
						index(page);
					},rd(500,1000));
				}
			}else{
				console.log('没有找到课程');
			}
        }
    });
}
function begin(id) {
	begin_time = new Date().getTime();
    $.ajax({
        type: "POST",
        url: "https://xzzf.gxrcpx.com:4443/api/internal.Train/course_data",
        data: 'id='+id+'&r='+begin_time,
		dataType:"json",
        error: function (obj) {
			console.log(JSON.stringify(obj));
			if(req_err<5){
				setTimeout(()=>begin(id),rd(1000,2000));
			}
        },
        success: function (obj) {
			console.log(obj);
			var data = obj.data;
			console.log('获取当前视频信息'+obj.message,'课程名称',watching_title,'视频id',data.id,'总时长',data.alltime,'已播放时长',data.alltime_user,'播放进度',((data.alltime_user/data.alltime)*100).toFixed(2)+'%');
			console.log('开始观看视频',watching_title);
			setTimeout(()=>{
				var watching_time2 = new Date().getTime();
				data.alltime_user = data.alltime_user + Math.round((watching_time2-begin_time)/1000)*mag+rt();
				watching(data);
			},rd(5000,10000));
        }
    });
}
function watching(data) {
	var cid = data.id;
	var alltime = data.alltime;
	var alltime_user = data.alltime_user;
	var rd_num = rt();
	if(alltime_user>alltime*1.02){alltime_user = alltime+rd_num}
	//console.log(alltime_user)
	watching_time = new Date().getTime();
    $.ajax({
        type: "POST",
        url: "https://xzzf.gxrcpx.com:4443/api/internal.Train/course_time",
        data: 'id='+cid+'&time='+alltime_user+'&r='+watching_time,
		dataType:"json",
        error: function (obj) {
			console.log(JSON.stringify(obj));
			if(req_err<5){
				setTimeout(()=>watching(data),rd(1000,2000));
			}
        },
        success: function (obj) {
			var percet;
			if(obj.data){
				percet = (obj.data.alltime_user/data.alltime);
				console.log('获取当前视频信息'+obj.message,'课程名称',watching_title,'完成课时',obj.data.classhours_user,'视频观看进度',(percet*100).toFixed(2)+'%');
			}
			var next_time = rd(3000,5000);
			if(alltime-alltime_user<(next_time/1000)){
				next_time = (alltime-alltime_user+alltime*0.01)*1000;
				console.log('当前课程准备完成');
			}
			if(alltime_user<alltime){
			setTimeout(()=>{
				var watching_time2 = new Date().getTime();
				data.alltime_user = data.alltime_user + Math.round((watching_time2-watching_time)/1000)*mag+rt();
				watching(data);
			},next_time);
			}else{
				console.log('当前课程已完成');
				get_total_hour();
				setTimeout(()=> index(1),200);
			}
        }
    });
}
function pause(data) {
	pause_time = new Date().getTime();
    $.ajax({
        type: "POST",
        url: "https://xzzf.gxrcpx.com:4443/api/internal.Train/course_time",
        data: 'id=4037789&time=2362.34191&r=1742355360808',
		dataType:"json",
        error: function (obj) {
			console.log(JSON.stringify(obj));
        },
        success: function (obj) {
			console.log(JSON.stringify(obj));
        }
    });
}
